<?php
/**
 * User: Jerry
 * Date: 2017/8/9
 * Time: 15:06
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class CommunityCommentGoodsModel
 * @author Jerry
 * @package SDXApi\Model
 */
class CommunityCommentGoodsModel extends Model
{
    /**
     * @var string
     */
    protected $tableName = 'comment_goods';

    /**
     * @author Jerry
     * @param int $start
     * @return mixed
     */
    public function findCommentGoods($start = 0)
    {
        $where = array(
            'cg.is_best'    =>  1,
            'g.is_delete'	=>	0,
            'g.is_on_sale'	=>	1,
            'g.original_img'=>	array(
                'NEQ',
                ''
            ),
        );

        $field = 'cg.comment_id, c.user_name, c.content, c.add_time as comment_time, g.goods_id, g.goods_name, g.goods_brief, g.goods_img, g.min_buy_num, g. spec_value, g.spec_unit';

        $commentJoin = 'left join sdx_comment as c on cg.comment_id = c.comment_id';
//        $commentImageJoin = 'left join sdx_comment_img as ci on cg.comment_id = ci.comment_id';
        $goodsJoin = 'left join sdx_goods as g on cg.goods_id = g.goods_id';

        $data = $this->field($field)
            ->alias('cg')
            ->join($commentJoin)
//            ->join($commentImageJoin)
            ->join($goodsJoin)
            ->where($where)
            ->group('cg.comment_id')
            ->limit($start, 20)
            ->select();
        return $data;
    }

    /**
     * @author Jerry
     * @return mixed
     */
    public function findCommentGoodsCount()
    {
        $where = array(
            'cg.is_best'    =>  1,
            'g.is_delete'	=>	0,
            'g.is_on_sale'	=>	1,
            'g.original_img'=>	array(
                'NEQ',
                ''
            ),
            'ci.img_name'   =>  array(
                'NEQ',
                ''
            ),
        );

        $field = 'cg.comment_id';

        $commentJoin = 'left join sdx_comment as c on cg.comment_id = c.comment_id';
        $commentImageJoin = 'left join sdx_comment_img as ci on cg.comment_id = ci.comment_id';
        $goodsJoin = 'left join sdx_goods as g on cg.goods_id = g.goods_id';

        $data = $this->field($field)
            ->alias('cg')
            ->join($commentJoin)
            ->join($commentImageJoin)
            ->join($goodsJoin)
            ->where($where)
            ->group('cg.comment_id')
            ->count($field);
        return $data;
    }

    /**
     * @author Jerry
     * @param int $commentId
     * @return mixed
     */
    public function findCommentImageByCommentId($commentId = 0)
    {
        $where = array(
            'comment_id' => $commentId,
        );

        return $this->table('sdx_comment_img')->field('img_name')->where($where)->limit(4)->select();
    }
}